package com.hysteria.game;
import java.util.ArrayList;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;

import com.hyteria.render.Renderable;
import com.hyteria.render.Renderer;


public class Game extends BasicGame{
	private static Game game;
    private static ArrayList<Behavior> behaviors = new ArrayList<Behavior>(); 
    
    public static void registerBehavior(Behavior b){ behaviors.add(b); }
    public static void removeBehavior(Behavior b){ behaviors.remove(b);}
    public static ArrayList<Behavior> getAllBehaviors(){ return behaviors; }
    
    
	private static boolean progressTurn = false; 
    
	private Map map;
	public Game() {
		super("Hysteria");
	}
    
    @Override
    public void init(GameContainer gc) 
			throws SlickException {
    	
    	map = new Map(25,19);
    	map.generate();
    	map.CApass();
    	map.CApass();
    	map.CApass();
    	map.CApass();
    	
    	Player.create(gc);
    	
    }
    
    public static Map getMap(){ return game.map; }
 
    @Override
    public void update(GameContainer gc, int delta) 
			throws SlickException     
    {
    	for(Behavior b : getAllBehaviors()){
			b.update();
		}
    	
    	if(progressTurn){
    		for(Behavior b: getAllBehaviors()){
    			b.onTurn();
    		}
    		
    		progressTurn = false;
    	}
    	
    	Renderer.update();
    }
 
    public void render(GameContainer gc, Graphics g) 
			throws SlickException 
    { 	
     	/*for(Renderable r : Renderer.getAll()){
     		r.draw(g);
     	}*/
     	Renderer.render(g);
    }
    
    //Handle keyboard input
    public void keyReleased(int key, char c){
    	if(key == Input.KEY_ESCAPE){
    		System.exit(0);
    	}
    }
 
    public void mouseClicked(int button, int x, int y, int count){
    	//nextTurn();
    }
    
    //Progress to the next turn.
    public static void progressTurn(){
    	progressTurn = true;
    }
    
    public static void main(String[] args) 
			throws SlickException
    {
    	game = new Game();
         AppGameContainer app = 
			new AppGameContainer(game);
 
         app.setDisplayMode(800, 600, false);
         app.setTargetFrameRate(30);
         //app.setVSync(true);
         app.start();
    }
}